import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class L18 {

    public static void main(String[] args) {
//        int[] nums = {1, 0, -1, 0, -2, 2};
//        int target = 0;
//        int[] nums = {-2, -1, -1, 1, 1, 2, 2};
        int[] nums = {1000000000,1000000000,1000000000,1000000000};
        int target = -294967296;
        List<List<Integer>> lists = new Solution().fourSum(nums, target);
        System.out.println(lists.toString());

    }

    static class Solution {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> lists = new ArrayList<>();

            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 2; i++) {

                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }

                for (int j = i + 1; j < nums.length - 1; j++) {
                    int left = j + 1;
                    int right = nums.length - 1;

                    if (j > i + 1 && nums[j] == nums[j - 1]) {
                        continue;
                    }

                    while (left < right) {
                        long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                        if (sum > target) {
                            right--;
                        } else if (sum < target) {
                            left++;
                        } else {
                            lists.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                            while (right > left && nums[left] == nums[left + 1]) left++;
                            while (right > left && nums[right] == nums[right - 1]) right--;
                            left++;
                            right--;
                        }
                    }
                }
            }

            return lists;
        }
    }
}
